package com.example.ch6;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SessionTestLogin
 */
public class SessionTestLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		response.setContentType("text/html");
		if (session.isNew()) {
			Cookie[] cookies = request.getCookies();
			if(cookies != null) {
				out.println("Cookies.Count = " + cookies.length + "<br />");
				for(Cookie cookie : cookies) {
					out.println("Name = " + cookie.getName() + " value = " + cookie.getValue() + "<br />");
					if(cookie.getName().equals("Name"))
						out.println("Welcom back " + cookie.getValue());
				}
			} else {
				RequestDispatcher dispatcher = request.getRequestDispatcher("login.html");
				dispatcher.forward(request, response);
			}
		} else {
			Cookie[] cookies = request.getCookies();
			for(Cookie cookie : cookies) {
				out.println("Name = " + cookie.getName() + " value = " + cookie.getValue() + "<br />");
				if(cookie.getName().equals("Name"))
					out.println("Welcom back " + cookie.getValue());
			}
		}
		/*
		HttpSession session = request.getSession(false);
		if(session !=null){
			// if session has been set, just show it.
			out.print(session.getId());
		}else{
			out.print("no session<br/>");
			HttpSession session2 = request.getSession();
			Collection<String> names =  response.getHeaderNames();
			for (String name : names) {
				if("Set-Cookie".equals(name)){
					// Get GMT timezone
					TimeZone zone = TimeZone.getTimeZone("GMT");
					// For testing purpose, I just set the expiration time as 2 minute after from now on.
					Date d = new Date(System.currentTimeMillis()+60*1000);
					// Set the date format in Set-Cookie header in accordance with HTTP specification.
					// Example: "Set-Cookie ...;Expires=Wed, 09 Jun 2021 10:18:14 GMT".
					SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
					// Set timezone as GMT. 
					//The SimpleDateFormat will convert the default timezone (in this machine is EST)
					// to GMT timezone.
					sdf.setTimeZone(zone);
					// Format the date.
					String date = sdf.format(d);
					// Append the expiration information to response header Set-Cookie
					response.setHeader(name, response.getHeader(name)+"; Expires="+date);
				}
			}
		}*/
	}

}
